package com.wyj.user.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyj.response.ApiCode;
import com.wyj.response.ApiResult;
import com.wyj.user.api.IUserApi;
import com.wyj.user.entity.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RefreshScope
@Slf4j
public class UserController implements IUserApi {
    private final RedisTemplate<Object, Object> redisTemplate;

    public UserController(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * TODO: 用户信息查询
     *
     * @param token token
     * @return com.wyj.user.entity.vo.UserVO
     * @author wuyijie
     * @date 2022/2/12 10:53 PM
     */
    @Override
    public ApiResult<UserVO> user(String token) {
        if (Boolean.TRUE.equals(redisTemplate.hasKey(token))) {
            final ObjectMapper objectMapper = new ObjectMapper();
            final Map<Object, Object> entries = redisTemplate.opsForHash().entries(token);
            return ApiResult.ok(objectMapper.convertValue(entries, UserVO.class));
        }
        return ApiResult.fail(ApiCode.BUSINESS_EXCEPTION, null);
    }
}
